﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Threading;


namespace MergeAndExecute._0
{
    public partial class Form1 : Form
    {
        public delegate void SetBoolean(Boolean enabled);
        public delegate void SetIntValue(Int32 val);
        public delegate void SetStringValue(String val);

        public delegate void InvokeSafe(Control control, Control root, Action action);

        private Boolean isAborted = false;
        private String fileNamesPath = String.Empty;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (var file in dlg.FileNames)
                {

                    using (TextReader reader = new StreamReader(file, Encoding.GetEncoding(1251)))
                    {
                        String content = reader.ReadToEnd();
                        richTextBox1.Text += Environment.NewLine;
                        richTextBox1.Text += Environment.NewLine;
                        richTextBox1.Text += "-- " + Path.GetFileName(file) + " --\n";
                        richTextBox1.Text += content;
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveDialog = new SaveFileDialog();
            // saveDialog.DefaultExt = ".sql";

            if (saveDialog.ShowDialog() == DialogResult.OK)
            {
                richTextBox1.SaveFile(saveDialog.FileName, RichTextBoxStreamType.PlainText);
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            richTextBox1.Text = String.Empty;
        }

        private void connectionSettingsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConnectionSettingsForm settingsForm = new ConnectionSettingsForm();
            settingsForm.ShowDialog();
        }

        private void SetMergeToolStripMenuItemEnabled(Boolean enabled)
        {
            if(this.menuStrip1.InvokeRequired)
            {
                this.Invoke(new SetBoolean(SetMergeToolStripMenuItemEnabled), enabled);
            }
            else
            {
                mergeToolStripMenuItem.Enabled = enabled;
            }
        }

        private void SetClearToolStripMenuItemEnabled(Boolean enabled)
        {
            if (this.menuStrip1.InvokeRequired)
            {
                this.Invoke(new SetBoolean(SetClearToolStripMenuItemEnabled), enabled );
            }
            else
            {
                clearToolStripMenuItem.Enabled = enabled;
            }
        }

        private void SetExecuteToolStripMenuItemEnabled(Boolean enabled)
        {
            if (this.menuStrip1.InvokeRequired)
            {
                this.Invoke(new SetBoolean(SetExecuteToolStripMenuItemEnabled), enabled);
            }
            else
            {
                executeToolStripMenuItem.Enabled = enabled;
            }
        }

        private void SetToolStripProgressBarValue(Int32 val)
        {
            if(this.InvokeRequired)
            {
                this.Invoke(new SetIntValue(SetToolStripProgressBarValue), val);
            }
            else
            {
                toolStripProgressBar1.Value = val;
            }
        }

        private void SetProgressBarVisible(Boolean visible)
        {
            if(this.InvokeRequired)
            {
                this.Invoke(new SetBoolean(SetProgressBarVisible), visible);
            }
            else
            {
                toolStripProgressBar1.Visible = visible;
            }
        }

        private void SetToolStripProgressBarMaxValue(Int32 val)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetIntValue(SetToolStripProgressBarMaxValue), val);
            }
            else
            {
                toolStripProgressBar1.Maximum = val;
            }
        }

        private void SetToolStripStatusText(String value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetStringValue(SetToolStripStatusText), value);
            }
            else
            {
                toolStripStatusLabel1.Text = value;
            }
        }

        private void SetRichTextboxText(String value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetStringValue(SetRichTextboxText), value);
            }
            else
            {
                this.richTextBox1.Text = value;
            }
        }

        private void AppendRichTextboxText(String value)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetStringValue(AppendRichTextboxText), value);
            }
            else
            {
                this.richTextBox1.Text += value;
            }
        }

        private void IncrementProgressBar(Int32 val)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new SetIntValue(IncrementProgressBar), val);
            }
            else
            {
                toolStripProgressBar1.Increment(val);
            }
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            var dlg = new OpenFileDialog();
            dlg.Multiselect = true;
            var fileslist = new SortedList<String, String>();
            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (var file in dlg.FileNames)
                {
                    fileslist.Add(Path.GetFileName(file), file);
                }
            }

            Thread execThread = new Thread(() =>
                                               {
                                                   SetMergeToolStripMenuItemEnabled(false);
                                                   SetClearToolStripMenuItemEnabled(false);
                                                   SetExecuteToolStripMenuItemEnabled(false);
                                                   SetToolStripProgressBarValue(0);
                                                   SetProgressBarVisible(true);
                                                   SetToolStripProgressBarMaxValue(fileslist.Count);
                                                   foreach (var kvp in fileslist)
                                                   {
                                                       SetToolStripStatusText("Executing " + kvp.Key);
                                                       AppendRichTextboxText("-- " + kvp.Key + " --\n");
                                                       AppendRichTextboxText(SQLExecutor.ExecuteScalar(kvp.Key, File.ReadAllText(kvp.Value, Encoding.GetEncoding(1251)))); ;
                                                       AppendRichTextboxText("------------------------------------------------------------------");
                                                       AppendRichTextboxText(Environment.NewLine);
                                                       AppendRichTextboxText(Environment.NewLine);
                                                       IncrementProgressBar(1);
                                                   }
                                                   SetMergeToolStripMenuItemEnabled(true);
                                                   SetClearToolStripMenuItemEnabled( true);
                                                   SetExecuteToolStripMenuItemEnabled(true);
                                                   SetProgressBarVisible(false);
                                                   SetToolStripStatusText("Ready");
                                               });
            execThread.IsBackground = true;
            execThread.Start();



        }

        private void Form1_Load(object sender, EventArgs e)
        {
            toolStripStatusLabel1.Text = "Ready";
            toolStripProgressBar1.Visible = false;
        }

        private void selectFilesAndMergeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button1_Click(sender, e);
        }

        private void selectFilesAndExecuteToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnExecute_Click(sender, e);
        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button2_Click(sender, e);
        }

        private void clearToolStripMenuItem_Click(object sender, EventArgs e)
        {
            button3_Click(sender, e);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void fileNamesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var dialog = new FolderBrowserDialog();
            dialog.SelectedPath = fileNamesPath;
            if(dialog.ShowDialog() == DialogResult.OK)
            {
                var folder = fileNamesPath = dialog.SelectedPath;
                var directoryInfo = new DirectoryInfo(folder);
                var filesInfo = directoryInfo.GetFiles();
                foreach (var fileInfo in filesInfo)
                {
                    richTextBox1.Text += Environment.NewLine;
                    richTextBox1.Text += Path.GetFileNameWithoutExtension(fileInfo.Name);

                }
                
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }
    }
}
